﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.Enums;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmToDoListSimpInfo : FormBase
    {
        private DBEntityToDoList todoListInfo = null;
        private DataSet dsEmployeeInfo;
        private ControlClassModeEnum _mode = ControlClassModeEnum.emInsert;
        private DBEntityReservation reservation = new DBEntityReservation();

        public ControlClassModeEnum Mode 
        {
            get { return _mode; }
            set { _mode = value; }
        }

        public frmToDoListSimpInfo(DataSet dsEmployee):base()
        {
            InitializeComponent();
            this.dsEmployeeInfo = dsEmployee;

        }
        public frmToDoListSimpInfo(DataSet dsEmployee, DBEntityToDoList todoList)
            :this(dsEmployee)
        {
            todoListInfo = (DBEntityToDoList)todoList.Clone();
            DataSet dsReservation = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryReservationForToDoListID(todoListInfo.TODOLISTID, out dsReservation, out error))
            {
                MsgBox.MsgInfo("获取预约信息错误：" + error);
                return;
            }
            if (dsReservation.Tables[0].Rows.Count != 0)
            {
                reservation = new DBEntityReservation(dsReservation.Tables[0].Rows[0]);
                btnTiao.Visible = true;
            }

        }
        private void frmToDoListInfo_Shown(object sender, EventArgs e)
        {
            toDoListInfoSimpControl1.InitDateControl(DateTime.Now, dsEmployeeInfo, todoListInfo);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            bool bFlag = false;
            if (_mode == ControlClassModeEnum.emInsert)
            {
                DBEntityToDoList todoInfo = new DBEntityToDoList();
                bFlag = toDoListInfoSimpControl1.GetToDoListInfo(ref todoInfo, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo(error);
                    return;
                }

                bFlag = BussinessProxyManager.Instance.InsertToDoList(todoInfo, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("新增待办事项失败\r\n" + error);
                    return;
                }
                else
                {
                    MsgBox.MsgInfo("保存成功。");
                    this.DialogResult = DialogResult.OK;
                }
            }
            else if (_mode == ControlClassModeEnum.emEdit)
            {
                bFlag = toDoListInfoSimpControl1.GetToDoListInfo(ref todoListInfo, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo(error);
                    return;
                }

                bFlag = BussinessProxyManager.Instance.UpdateToDoList(todoListInfo, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("更新代办事项失败\r\n" + error);
                    return;
                }
                else
                {
                    MsgBox.MsgInfo("保存成功。");
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
           //reservation
            frmAdjustReservation adjReservation = new frmAdjustReservation(reservation);
            if (adjReservation.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string filter = DBEntityToDoList.FieldName.TODOLISTID + " = " + todoListInfo.TODOLISTID;
            DataSet todoList = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryTodoList(filter, out todoList, out error))
            {
                MsgBox.MsgInfo("获取提示信息错误：" + error);
                return;
            }
            
            if (todoList.Tables[0].Rows.Count != 0)
            {
                todoListInfo.TODOCOMPLETIONRATE = 100;
                BussinessProxyManager.Instance.UpdateToDoList(todoListInfo, out error);
            }
            MsgBox.MsgInfo("保存成功。");
            this.DialogResult = DialogResult.OK;
        }
    }
}
